package com.ldq.imooc.basics.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/4/23 16:38
 * @Modified by :
 */
public class MultiTCPServer {

    public static void main(String[] args) {

        try {
            //创建服务器Socket
            ServerSocket server = new ServerSocket(8888);
            Socket socket = null;
            //记录客户端的数量
            int count = 0;
            System.out.println("***服务器正在启动，等待客户端连接***");
            //循环监听，等待客户端连接
            while (true) {
                //创建监听，等待连接
                socket = server.accept();
                //创建新的线程
                TCPServerThread TCPServerThread = new TCPServerThread(socket);
                //启动线程
                TCPServerThread.start();

                count++;
                System.out.println("当前客户端数量："+count);

                InetAddress address = socket.getInetAddress();
                System.out.println("客户端IP："+address.getHostAddress());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}